import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import java.io.IOException;

//配置Filter拦截路径
@WebFilter("/test")
public class filter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        request.setCharacterEncoding("utf-8");
        String[] str  = new String[]{"sb","lz","2货"};
        String name = request.getParameter("name");
//        name = name.replaceAll("sb","**");
//        name = name.replaceAll("lz","**");
        for (int i = 0; i < str.length; i++) {
            String index = str[i];
            name = name.replaceAll(index,"**");
        }
        System.out.print("过滤后输出：");
        System.out.println(name);
        request.setAttribute("name",name);


        chain.doFilter(request,response);
    }
    @Override
    public void destroy() {

    }
}
